#include <iostream>
#include <sw/redis++/redis.h>
#include <vector>
#include <sw/redis++/redis++.h>
#include <string>
#include <unordered_map>
#include "util.hpp"

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::unordered_map;
using std::vector;
using sw::redis::Redis;

void test1(Redis &redis)
{
    cout << "zadd and zrange" << endl;
    redis.flushall();

    redis.zadd("key", "hmcjn", 96);
    redis.zadd("key", {std::make_pair("hmcjn", 96),
                       std::make_pair("ppp", 97)});
    vector<std::pair<string, double>> members = {
        std::make_pair("yyy", 98),
        std::make_pair("zb", 99)};
    redis.zadd("key", members.begin(), members.end());

    vector<std::pair<string, double>> result;
    auto it = std::back_inserter(result);
    redis.zrange("key", 0, -1, it);
    printContinerPair(result);
}

void test2(Redis &redis)
{
    cout << "zcard and zrem and zscore and zrank" << endl;
    redis.flushall();

    redis.zadd("key", "hmcjn", 96);
    redis.zadd("key", {std::make_pair("hmcjn", 96),
                       std::make_pair("ppp", 97)});
    vector<std::pair<string, double>> members = {
        std::make_pair("yyy", 98),
        std::make_pair("zb", 99)};
    redis.zadd("key", members.begin(), members.end());

    cout << "zcar and zrem: " << endl;
    cout  << "zcard: " << redis.zcard("key") << endl;
    cout << "zrem: " << redis.zrem("key", "ppp") << endl;
    cout  << "zcard: " << redis.zcard("key") << endl << endl;

    auto score = redis.zscore("key", "hmcjn");
    if(score) cout << "zscore: " << score.value() << endl << endl;
    else cout << "元素无效" << endl;

    auto rank = redis.zrank("key","yyy");
    if(rank) cout << "zrank: " << rank.value() << endl << endl;
    else cout << "元素无效" << endl;

    auto rank1 = redis.zrank("key","zb");
    if(rank1) cout << "zrank1: " << rank1.value() << endl << endl;
    else cout << "元素无效" << endl;
}

int main()
{
    Redis redis("tcp://127.0.0.1:6379");

    // test1(redis);
    test2(redis);
    return 0;
}